package com.drought.rained.auth;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.text.format.Time;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.drought.rained.R;
import com.drought.rained.base.BaseTitleActivity;
import com.drought.rained.bean.AuthInfoDataLJ;
import com.drought.rained.bean.DictionaryItemDataLJ;
import com.drought.rained.business.AuthRouterLJ;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.cache.UserInfoLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.dao.LocationDaoLJ;
import com.drought.rained.mvpview.DictionaryMvpViewLJ;
import com.drought.rained.mvpview.WorkAuthMvpViewLJ;
import com.drought.rained.presenter.DictionaryPresenterLJ;
import com.drought.rained.presenter.WorkAuthPresenterLJ;
import com.drought.rained.response.AuthInfoResponseLJ;
import com.drought.rained.response.BlankResponseLJ;
import com.drought.rained.utils.ToastUtilsLJ;
import com.drought.rained.view.LocationDialogLJ;
import com.drought.rained.view.MenuDialogLJ;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.picker.DateTimePicker;

@Route(name = "工作信息认证", path = RouterMapLJ.WORK_AUTH_PAGE)
public class WorkAuthActivityLJ extends BaseTitleActivity implements DictionaryMvpViewLJ, WorkAuthMvpViewLJ {

    @BindView(R.id.et_company_name)
    EditText etCompanyName;
    @BindView(R.id.et_work_type)
    EditText etWorkType;
    @BindView(R.id.et_job_level)
    EditText etJobLevel;
    @BindView(R.id.et_work_age)
    EditText etWorkAge;
    @BindView(R.id.et_salary)
    EditText etSalary;
    @BindView(R.id.et_company_tel)
    EditText etCompanyTel;
    @BindView(R.id.et_company_net)
    EditText etCompanyNet;
    @BindView(R.id.et_province)
    EditText etProvince;
    @BindView(R.id.et_city)
    EditText etCity;
    @BindView(R.id.et_town)
    EditText etTown;
    @BindView(R.id.et_area)
    EditText etArea;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.et_post)
    EditText etPost;
    @BindView(R.id.tv_next_step)
    TextView tvNextStep;
    @Autowired(name = ConstantLJ.IS_BACK)
    boolean is_back;
    private DictionaryPresenterLJ dictionaryPresenter;
    private WorkAuthPresenterLJ workAuthPresenter;
    private DatePicker picker;
    private LocationDialogLJ mLocationDialog;
    private ArrayList<DictionaryItemDataLJ> mWorkTypeList;
    private ArrayList<DictionaryItemDataLJ> mSalaryList;
    private ArrayList<DictionaryItemDataLJ> mJobLevelList;
    private MenuDialogLJ mMenuDialog;
    private final String DICTIONARY_TYPE_WORK_TYPE = "common_professional"; // 工作类型
    private final String DICTIONARY_TYPE_SALARY_TREATMENT = "salary_treatment"; // 工资
    private final String DICTIONARY_TYPE_JOB_LEVEL = "current_position"; // 目前职位
    private ArrayList<String> mProvinces;
    private ArrayList<String> mCitys;
    private ArrayList<String> mTowns;
    private ArrayList<String> mAreas;
    private LocationDaoLJ dao;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onUIReady() {
        ARouter.getInstance().inject(this);
        workAuthPresenter = new WorkAuthPresenterLJ(this);
        if (UserInfoLJ.isShowUserInformation())
            workAuthPresenter.getWorkAuthInfo();
        if (is_back) {
            tvNextStep.setText(R.string.save_change);
        }

        dictionaryPresenter = new DictionaryPresenterLJ(this);
        List<String> types = new ArrayList<>();
        types.add(DICTIONARY_TYPE_WORK_TYPE);
        types.add(DICTIONARY_TYPE_JOB_LEVEL);
        types.add(DICTIONARY_TYPE_SALARY_TREATMENT);
        dictionaryPresenter.getDictionaryData(types);
        showLoadingView();

        dao = new LocationDaoLJ();
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_work_auth;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.work_auth_page_name);
    }

    @OnClick({R.id.tv_next_step})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_next_step:
                commit();
                break;
        }
    }

    private void commit() {
        String companyName = etCompanyName.getText().toString().trim();
        String workType = etWorkType.getText().toString().trim();
        String workTypeCode = (String) etWorkType.getTag();
        String jobLevel = etJobLevel.getText().toString().trim();
        String jobLevelCode = (String) etJobLevel.getTag();
        String workAge = etWorkAge.getText().toString().trim();
        String salary = etSalary.getText().toString().trim();
        String salaryCode = (String) etSalary.getTag();
        String companyTel = etCompanyTel.getText().toString().trim();
        String companyNet = etCompanyNet.getText().toString().trim();
        String province = etProvince.getText().toString().trim();
        String city = etCity.getText().toString().trim();
        String town = etTown.getText().toString().trim();
        String area = etArea.getText().toString().trim();
        String postNo = etPost.getText().toString().trim();
        String address = etAddress.getText().toString().trim();
        if (TextUtils.isEmpty(companyName)) {
            ToastUtilsLJ.showShort(this, R.string.work_auth_company_name_hint);
            return;
        } else if (TextUtils.isEmpty(workType) || TextUtils.isEmpty(workTypeCode)) {
            ToastUtilsLJ.showShort(this, R.string.work_auth_work_type_hint);
            return;
        } else if (TextUtils.isEmpty(jobLevel) || TextUtils.isEmpty(jobLevelCode)) {
            ToastUtilsLJ.showShort(this, R.string.work_auth_job_level_hint);
            return;
        } else if (TextUtils.isEmpty(workAge)) {
            ToastUtilsLJ.showShort(this, R.string.work_auth_work_age_hint);
            return;
        } else if (TextUtils.isEmpty(salary) || TextUtils.isEmpty(salaryCode)) {
            ToastUtilsLJ.showShort(this, R.string.work_auth_salary_hint);
            return;
        } else if (TextUtils.isEmpty(companyTel)) {
            ToastUtilsLJ.showShort(this, R.string.work_auth_company_tel_hint);
            return;
        } else if (TextUtils.isEmpty(companyNet)) {
            ToastUtilsLJ.showShort(this, R.string.work_auth_company_net_hint);
            return;
        } else if (TextUtils.isEmpty(province)) {
            ToastUtilsLJ.showShort(this, R.string.province_hint);
            return;
        } else if (TextUtils.isEmpty(city)) {
            ToastUtilsLJ.showShort(this, R.string.work_auth_city_hint);
            return;
        } else if (TextUtils.isEmpty(town)) {
            ToastUtilsLJ.showShort(this, R.string.work_auth_company_town_hint);
            return;
        } else if (TextUtils.isEmpty(area)) {
            ToastUtilsLJ.showShort(this, R.string.work_auth_company_area_hint);
            return;
        } else if (TextUtils.isEmpty(address)) {
            ToastUtilsLJ.showShort(this, R.string.auth_type_company_address_hint);
            return;
        } else if (TextUtils.isEmpty(postNo)) {
            ToastUtilsLJ.showShort(this, R.string.work_auth_company_post_hint);
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("authPageCode", AuthRouterLJ.getInstance().routerMap2Code(RouterMapLJ.WORK_AUTH_PAGE));
        map.put("companyName", companyName);
        map.put("companyIndustryCode", workTypeCode);
        map.put("commonProfessional", jobLevelCode);
        map.put("workStartTime", workAge);
        map.put("salary", salaryCode);
        map.put("companyTel", companyTel);
        map.put("companyNet", companyNet);
        map.put("companyProvince", province);
        map.put("companyCity", city);
        map.put("companyTown", town);
        map.put("companyRegion", area);
        map.put("companyAddressDesc", address);
        map.put("companyZipcode", postNo);


        workAuthPresenter.saveAuthInfo(map);
        showLoadingView();
    }

    @OnClick({R.id.rl_province, R.id.rl_city, R.id.rl_town, R.id.rl_area,
            R.id.et_province, R.id.et_city, R.id.et_town, R.id.et_area})
    public void getLocation(View view) {
        switch (view.getId()) {
            case R.id.rl_province:
            case R.id.et_province:
                pickerProvince();
                break;
            case R.id.rl_city:
            case R.id.et_city:
                pickerCity();
                break;
            case R.id.rl_town:
            case R.id.et_town:
                pickerTown();
                break;
            case R.id.rl_area:
            case R.id.et_area:
                pickerArea();
                break;
        }
    }

    private void pickerProvince() {
        mProvinces = dao.getProvince();
        getLocationDialog().showDialog(etProvince, mProvinces);
    }

    private void pickerCity() {
        String province = etProvince.getText().toString();
        mCitys = dao.getCity(province);
        getLocationDialog().showDialog(etCity, mCitys);
    }

    private void pickerTown() {
        String province = etProvince.getText().toString();
        String city = etCity.getText().toString();
        mTowns = dao.getTown(province, city);
        getLocationDialog().showDialog(etTown, mTowns);
    }

    private void pickerArea() {
        String province = etProvince.getText().toString();
        String city = etCity.getText().toString();
        String town = etTown.getText().toString();
        mAreas = dao.getArea(province, city, town);
        getLocationDialog().showDialog(etArea, mAreas);
    }

    @OnClick({R.id.rl_work_type, R.id.rl_job_level, R.id.rl_work_age, R.id.rl_salary,
            R.id.et_work_type, R.id.et_job_level, R.id.et_work_age, R.id.et_salary})
    public void getDictionary(View view) {
        switch (view.getId()) {
            case R.id.rl_work_type:
            case R.id.et_work_type:
                picker_work_type();
                break;
            case R.id.rl_job_level:
            case R.id.et_job_level:
                picker_job_level();
                break;
            case R.id.rl_work_age:
            case R.id.et_work_age:
                pickerTime();
                break;
            case R.id.rl_salary:
            case R.id.et_salary:
                pickSalary();
                break;
        }
    }

    private void picker_work_type() {
        if (mWorkTypeList == null || mWorkTypeList.size() == 0) {
            return;
        }
        getMenuDialog().showDialog(etWorkType, mWorkTypeList);
    }

    private void picker_job_level() {
        if (mJobLevelList == null || mJobLevelList.size() == 0) {
            return;
        }
        getMenuDialog().showDialog(etJobLevel, mJobLevelList);
    }

    private void pickSalary() {
        if (mSalaryList == null || mSalaryList.size() == 0) {
            return;
        }
        getMenuDialog().showDialog(etSalary, mSalaryList);
    }

    @SuppressLint("SetTextI18n")
    private void pickerTime() {
        if (picker == null) {
            //24小时值
            Time time = new Time();
            time.setToNow();
            int year = time.year;
            int month = time.month + 1;
            int day = time.monthDay;
            picker = new DatePicker(this, DateTimePicker.YEAR_MONTH_DAY);
            picker.setLabel(getStringResource(R.string.year_unit),
                    getStringResource(R.string.month_unit),
                    getStringResource(R.string.day_unit));
            picker.setRangeStart(year - 20, 1, 1);//日期起点
            picker.setSelectedItem(year, 1, 1); // 设置默认选中值
            picker.setRangeEnd(year, month, day);//日期终点
            picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
                @Override
                public void onDatePicked(String year, String month, String day) {
                    etWorkAge.setText(year + "-" + month);
                }
            });
        }
        picker.show();
    }

    @Override
    public void onGetDataNext(BlankResponseLJ response) {
        dismissLoadingView();
        if (!is_back) {
            AuthRouterLJ.getInstance().jumpNextPager(this, RouterMapLJ.WORK_AUTH_PAGE);
        } else {
            finish();
        }

    }

    public MenuDialogLJ getMenuDialog() {
        if (mMenuDialog == null) {
            mMenuDialog = new MenuDialogLJ(this, R.style.dialog_bottom_full);
        }
        return mMenuDialog;
    }

    public LocationDialogLJ getLocationDialog() {
        if (mLocationDialog == null) {
            mLocationDialog = new LocationDialogLJ(this, R.style.dialog_bottom_full);
        }

        return mLocationDialog;
    }

    @Override
    public void onGetDataNext(AuthInfoResponseLJ response) {
        fillTextView(response.getData().getCompanyInfo());

    }

    private void fillTextView(AuthInfoDataLJ.CompanyInfoBean companyInfo) {
        etCompanyName.setText(companyInfo.getCompanyName());
        etCompanyNet.setText(companyInfo.getCompanyNet());
        etCompanyTel.setText(companyInfo.getCompanyTel());
        etWorkAge.setText(companyInfo.getWorkStartTime());

        etWorkType.setText(companyInfo.getCompanyIndustryValue());
        etWorkType.setTag(companyInfo.getCompanyIndustryCode());
        etJobLevel.setText(companyInfo.getCommonProfessionalValue());
        etJobLevel.setTag(companyInfo.getCommonProfessional());
        etSalary.setText(companyInfo.getSalaryValue());
        etSalary.setTag(companyInfo.getSalary());

        etProvince.setText(companyInfo.getCompanyProvince());
        etCity.setText(companyInfo.getCompanyCity());
        etTown.setText(companyInfo.getCompanyTown());
        etArea.setText(companyInfo.getCompanyRegion());
        etAddress.setText(companyInfo.getCompanyAddressDesc());
        etPost.setText(companyInfo.getCompanyZipcode());
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }

    @Override
    public void onGetDataNext(ArrayList<ArrayList<DictionaryItemDataLJ>> responseWrapper) {
        dismissLoadingView();
        if (responseWrapper.size() >= 3) {
            mWorkTypeList = responseWrapper.get(0);
            mJobLevelList = responseWrapper.get(1);
            mSalaryList = responseWrapper.get(2);
        }
    }
}
